package ltd.followkba.page;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kcbsdk.EventHandler;

import ltd.advskin.MSKIN;
import ltd.advskin.base.BasePage;
import ltd.advskin.base.KcTypeListener;
import ltd.advskin.utils.SubUtils;
import ltd.advskin.view.KcHeaderView;
import ltd.followkba.MAPP;
import ltd.followkba.R;
import ltd.followkba.W;
import ltd.followkba.task.BDbsAccount;
import ltd.followkba.task.LogTyp;
import ltd.followkba.task.RunUtils;
import main.MAPI;

import static ltd.advskin.MSKIN.MSTRING;
import static ltd.advskin.MSKIN.MTOAST;
import static main.MAPI.mMyWorks;

public class AutoGiftPage extends BasePage {
    private EditText etGift0;
    private EditText etGift1;
    private EditText etGift2;
    private EditText etGift3;
    private TextView tvGift9;

    // 所有的页面都接受同样的方式打开LOG
    public static boolean logEnable;

    public void MLOG(String text) {
        if (logEnable) {
            MSTRING(MAPI.GET_LOG_NAME(this) + text);
        }
    }

    @Override
    public void onInitView() {                       // 使用者需要继承，初始化页面控件等显示
        setLayout(R.layout.page_auto_gift);
        mKcHeaderView = (KcHeaderView) findViewById(R.id.khvGift);
        mKcHeaderView.setTitle("设置自动送礼物", new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type) {
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        onFinish();
                        break;

                }
            }
        });
        etGift0 = findViewById(R.id.etGift0);
        etGift1 = findViewById(R.id.etGift1);
        etGift2 = findViewById(R.id.etGift2);
        etGift3 = findViewById(R.id.etGift3);
        tvGift9 = findViewById(R.id.tvGift9);
        findViewById(R.id.kbrSaveGift).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 保存上面的条件
                if (MAPP.mBDbsAccount.GiftSet == null){
                    MAPP.mBDbsAccount.GiftSet = new int[5];
                    for (int index = 0; index < MAPP.mBDbsAccount.GiftSet.length; index++){
                        MAPP.mBDbsAccount.GiftSet[index] = -1;
                    }
                }
                for (int index = 0; index < 4; index++) {
                    saveGiftSet(index);
                }
                boolean negative = true;
                for (int index = 0; index < MAPP.mBDbsAccount.GiftSet.length; index++){
                    if (MAPP.mBDbsAccount.GiftSet[index] != -1){
                        negative = false;
                        break;
                    }
                }
                if (negative){              // 全部都是-1
                    MAPP.mBDbsAccount.GiftSet = null;
                }
                MTOAST("已保存设置");
                RunUtils.saveBDbsAccount(MAPP.mBDbsAccount);
            }
        });

        findViewById(R.id.kbtGift9).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 第9行保存
                MAPP.startCommentTextPage(LogTyp.GIFT_TEXT);
            }
        });


    }
    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据
        showInfo();
    }
    @Override
    public void onFinish() {       // 使用者选择性继承，用户按了后退键，不继承就直接关闭了
        SubUtils subUtils = new SubUtils();
        subUtils.hideInputMethod();
        setClose();
    }

    private void showInfo(){
        if (MAPP.mBDbsAccount.GiftSet != null && MAPP.mBDbsAccount.GiftSet.length >= 4){
            MSKIN.setText(String.format("%s", MAPP.mBDbsAccount.GiftSet[0]), etGift0);
            MSKIN.setText(String.format("%s", MAPP.mBDbsAccount.GiftSet[1]), etGift1);
            MSKIN.setText(String.format("%s", MAPP.mBDbsAccount.GiftSet[2]), etGift2);
            MSKIN.setText(String.format("%s", MAPP.mBDbsAccount.GiftSet[3]), etGift3);

        }else {
            MSKIN.setText("-1", etGift0);
            MSKIN.setText("-1", etGift1);
            MSKIN.setText("-1", etGift2);
            MSKIN.setText("-1", etGift3);
        }
        MSKIN.setText(MAPP.mBDbsAccount.TextComment[W.TEXT_GIFT], tvGift9);
//        MSKIN.setText(MAPP.textsToText(MAPP.mBDbsAccount.GiftText), tvGift9);
    }
    private void saveGiftSet(int index){
        EditText[] editTexts = {etGift0, etGift1, etGift2, etGift3};
        String text = editTexts[index].getText().toString();
        int value = "-1".equals(text) ? -1 : MAPI.STRING_DEC32(text);
        MAPP.mBDbsAccount.GiftSet[index] = value;
    }

}
